<template>
	<view class="container">
		<u-no-network tips="无网络访问权限, 或尚未连接互联网"></u-no-network>
		<view class="board">
			<view class="title">
				您好，
				<br />
				欢迎使用{{ systemName }}
			</view>
		</view>
		<view class="login-view">
			<view class="content">
				<u-subsection
					v-if="loginType === 'both'"
					:list="section"
					:current="form.type"
					@change="onSectionChange"
				>
				</u-subsection>
				<view class="form">
					<template v-if="form.type === 0">
						<view class="form-line">
							<text class="txt">手机号</text>
							<view class="input">
								<input
									type="text"
									placeholder="请输入您的手机号"
									v-model="form.mobile"
								/>
							</view>
						</view>
						<view class="form-line">
							<text class="txt">验证码</text>
							<view class="input">
								<input
									type="text"
									placeholder="请输入验证码"
									v-model="form.code"
								/>
								<u-button
									type="primary"
									size="small"
									:text="countDown ? countDown + '秒后重试' : '发送验证码'"
									:disabled="!!countDown"
									:plain="true"
									@click="onSendMsg"
								>
								</u-button>
							</view>
						</view>
					</template>
					<template v-else>
						<view class="form-line">
							<text class="txt">账号</text>
							<view class="input">
								<input
									type="text"
									placeholder="请输入您的登录账号"
									v-model="form.passport"
								/>
							</view>
						</view>
						<view class="form-line">
							<text class="txt">密码</text>
							<view class="input">
								<input
									type="password"
									placeholder="请输入您的登录密码"
									v-model="form.password"
									password
								/>
							</view>
						</view>
					</template>
					<view class="buttons">
						<button class="login" @tap="onLogin">登 录</button>
					</view>
				</view>
			</view>
		</view>
		<text v-if="copyright" class="copyright">{{ copyright }}</text>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>
<script>
import {
	getSystemConfigName,
	getLoginPageType,
	getLoginPageCopyright,
} from '@/src/config.js'
import {
	accountLogin,
	mobileLogin,
	sendMobileMsg,
	getLoginUserInfo,
} from '@/src/api/account.js'

export default {
	data() {
		return {
			loginType: getLoginPageType(),
			section: ['手机登录', '密码登录'],
			form: {
				type: 0,
				passport: '',
				password: '',
				mobile: '',
				code: '',
			},
			countDown: 0,
			systemName: getSystemConfigName(),
			copyright: '',
		}
	},
	onLoad() {
		const loginCopyright = getLoginPageCopyright()
		if (loginCopyright === 'version') {
			this.copyright =
				'v' +
				(uni.getAppBaseInfo().appWgtVersion || uni.getAppBaseInfo().appVersion)
		} else {
			this.copyright = loginCopyright
		}
		if (this.loginType === 'account') {
			this.form.type = 1
		}
	},
	methods: {
		// 切换登录方式
		onSectionChange(index) {
			this.form.type = index
		},
		// 是否手机登录（否则为账密登录)
		isMobileLogin() {
			if (this.loginType === 'both') {
				return this.form.type === 0
			}
			return this.loginType === 'mobile'
		},
		// 发送验证码
		onSendMsg() {
			if (!this.form.mobile) {
				return this.errorTips('请输入手机号码')
			} else if (!uni.$u.test.mobile(this.form.mobile)) {
				return this.errorTips('手机号格式有误')
			}
			uni.showLoading({
				title: '正在发送',
			})
			sendMobileMsg(this.form.mobile)
				.then((res) => {
					uni.showToast({
						title: res.message,
					})
					this.countDown = 60
					const running = setInterval(() => {
						this.countDown--
						if (this.countDown <= 0) {
							clearInterval(running)
						}
					}, 1000)
				})
				.catch((err) => {
					uni.hideLoading()
					this.errorTips(err.message || '请求失败，请稍候重试')
				})
		},
		// 用户登录
		async onLogin() {
			if (this.isMobileLogin()) {
				if (!this.form.mobile) {
					return this.errorTips('请输入手机号码')
				} else if (!uni.$u.test.mobile(this.form.mobile)) {
					return this.errorTips('手机号格式有误')
				} else if (!this.form.code) {
					return this.errorTips('请输入验证码')
				}
			} else {
				if (!this.form.passport) {
					return this.errorTips('请输入您的登录账号')
				} else if (!this.form.password) {
					return this.errorTips('请输入您的登录密码')
				}
			}
			uni.showLoading({
				title: '正在登录',
			})
			try {
				let tokenRes
				if (this.isMobileLogin()) {
					// 手机登录
					tokenRes = await mobileLogin(this.form.mobile, this.form.code)
				} else {
					// 密码登录
					tokenRes = await accountLogin(this.form.passport, this.form.password)
				}
				// 登录成功
				this.$store.dispatch('writeUserToken', tokenRes.data.Token)
				// 读取用户信息并写入初始数据
				const userRes = await getLoginUserInfo(tokenRes.data.Token)
				this.$store.dispatch('saveUserInfo', userRes.data)
				uni.hideLoading()
				uni.switchTab({
					url: '/pages/dashboard/dashboard',
				})
			} catch (e) {
				console.log(e)
				this.$store.dispatch('userLogout')
				uni.hideLoading()
				this.errorTips(e.message || '请求失败，请稍候重试')
			}
		},
		// 错误提示
		errorTips(message) {
			this.$refs.uToast.show({
				type: 'error',
				icon: 'close-circle',
				position: 'top',
				message,
				duration: 1500,
			})
		},
	},
}
</script>
<style lang="scss" scoped>
page {
	background-color: #fff;
}

.board {
	width: 100%;
	height: 450rpx;
	background-image: url(/static/login-head.png);
	background-size: 100%;

	.title {
		transform-origin: left;
		font-size: 42rpx;
		color: #ffffff;
		padding: 130rpx 0 0 70rpx;
		font-weight: bold;
		line-height: 70rpx;
	}
}

.login-view {
	width: 100%;
	position: relative;
	margin-top: -120rpx;
	background-color: #ffffff;
	border-radius: 4% 4% 0% 0;

	.content {
		margin: 0 60rpx;
		font-size: 28rpx;
		padding-top: 40rpx;
		padding-bottom: 40rpx;

		.uni-input-placeholder {
			color: #aeaeae;
		}

		.form {
			padding-top: 40rpx;

			.form-line {
				position: relative;

				.txt {
					font-size: 32rpx;
					font-weight: bold;
					color: #333333;
				}

				.input {
					display: flex;
					height: 90rpx;
					line-height: 90rpx;
					margin-bottom: 50rpx;
					border-bottom: 1px solid #e9e9e9;
					font-size: 28rpx;

					input {
						flex: 1;
						height: 90rpx;
						line-height: 90rpx;
					}

					.u-button {
						position: relative;
						top: 15rpx;
						width: 100px;
						margin-left: 20rpx;
					}
				}
			}
		}

		.buttons {
			.login {
				font-size: 28rpx;
				background: #2796f2;
				color: #fff;
				height: 80rpx;
				line-height: 80rpx;
				border-radius: 20rpx;
				font-weight: bold;
			}

			.reg {
				font-size: 28rpx;
				color: #fff;
				height: 80rpx;
				line-height: 80rpx;
				border-radius: 20rpx;
				font-weight: bold;
				background: #f5f6fa;
				color: #000000;
				transform-origin: center;
				margin-top: 30rpx;

				&:after {
					border: none;
				}
			}
		}
	}
}

.copyright {
	position: absolute;
	z-index: 2;
	right: 0;
	bottom: 0;
	transform-origin: right;
	font-size: 12px;
	color: #666;
	padding: 10px;
}
</style>
